package com.mj.hotel.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.mj.hotel.di.component.DaggerMainComponent;
import com.mj.hotel.mvp.contract.MainContract;
import com.mj.hotel.mvp.presenter.MainPresenter;

import com.mj.hotel.R;
import com.mj.hotel.mvp.ui.event.ChangeFragmentMsg;
import com.mj.hotel.mvp.ui.fragment.HomeFragment;
import com.mj.hotel.mvp.ui.fragment.RoomDetailFragment;
import com.mj.hotel.mvp.ui.fragment.RoomListFragment;
import com.mj.hotel.mvp.ui.fragment.SplashFragment;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    @BindView(R.id.header)
    public View header;
    @BindView(R.id.footer)
    public View footer;
    @BindView(R.id.container)
    public FrameLayout coutainer;

    private FragmentManager fm;
    private Fragment splashFragment;
    private Fragment homeFragment;
    private Fragment roomListFragment;
    private Fragment roomDetailFragment;
    private HashMap<String, BaseFragment> fragmentHashMap = new HashMap<String,BaseFragment>();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        fm = getSupportFragmentManager();
        splashFragment = new SplashFragment();
        homeFragment = new HomeFragment();
        roomListFragment = new RoomListFragment();
        roomDetailFragment = new RoomDetailFragment();
        fragmentHashMap.put(SplashFragment.class.getSimpleName(), (BaseFragment) splashFragment);
        fragmentHashMap.put(HomeFragment.class.getSimpleName(), (BaseFragment) homeFragment);
        fragmentHashMap.put(RoomListFragment.class.getSimpleName(),(BaseFragment)roomListFragment);
        fragmentHashMap.put(RoomDetailFragment.class.getSimpleName(),(BaseFragment)roomDetailFragment);

        changeFragment(ChangeFragmentMsg.getInstance(SplashFragment.class.getSimpleName()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Subscriber
    private void changeFragment(ChangeFragmentMsg changeFragmentMsg){
        String msg = changeFragmentMsg.message;
        if(msg.equals(SplashFragment.class.getSimpleName())){
            footer.setVisibility(View.GONE);
            header.setVisibility(View.GONE);
        }else{
            footer.setVisibility(View.VISIBLE);
            header.setVisibility(View.VISIBLE);
        }
        BaseFragment fragment = fragmentHashMap.get(changeFragmentMsg.message);
        showFragment(fragment);
    }

    private void showFragment(Fragment fragment){
        FragmentTransaction transaction = fm.beginTransaction();
        hideAllFragement();
        transaction.add(R.id.container,fragment);
        transaction.show(fragment);
        transaction.commit();
    }
    public void hideAllFragement(){
        FragmentTransaction transaction = fm.beginTransaction();
        Iterator<Map.Entry<String, BaseFragment>> entries = fragmentHashMap.entrySet().iterator();
        while(entries.hasNext()){
            Map.Entry<String, BaseFragment> entry = entries.next();
            Fragment fragment = entry.getValue();
            transaction.hide(fragment);
        }
        transaction.commit();
    }

}
